class IncidentsController < ApplicationController
  
  before_filter :ensure_login
    def index
        #@Incidents = Incident.all
    	  @Machines = Machine.all
    	  @Incidents = Incident.paginate(:page => params[:page])
    end
  
    def show
        @start = params[:start]
        @end = params[:end]
        @machine = Machine.find(params[:machine_selection][:machine_id])
        @incidentsPie = Incident.find_by_sql(["select incident_type, count(*) As resultat from incidents where machine_id = ? AND created_at BETWEEN ? and ? group by incident_type", params[:machine_selection][:machine_id], @start, @end])
        @incidentsTauxCloture = Incident.find_by_sql(["select incident_type, count(*) As resultat from incidents where machine_id = ? AND created_at BETWEEN ? and ? AND resolution_date IS NOT NULL group by incident_type", params[:machine_selection][:machine_id], @start, @end])
        
        @incidentsHist = Incident.find_by_sql(["SELECT incident_type, DATE_FORMAT(created_at, '%m/%Y') As date_anomalie, count(*) AS resultat FROM incidents WHERE machine_id = ? AND created_at BETWEEN ? and ? group by incident_type, DATE_FORMAT(created_at, '%m/%Y') ORDER BY DATE_FORMAT(created_at, '%m/%Y')", params[:machine_selection][:machine_id], @start, @end])
    end
    
    def closeIncident
        incident_id = params[:id]
        @incident = Incident.update_all({:resolution_date => Time.now}, "id = #{incident_id}")
        if @incident
          message = "Mis à jour effectuée"
        else
          message = "problème lors de la mise à jour"
        end  
        flash[:notice] = message
        
        redirect_to :controller => "incidents", :action => "index"
    end
    
    def closeAllIncident
        incident_id = params[:id]
        @info_incident = Incident.find(params[:id])
        @incident = Incident.update_all({:resolution_date => Time.now}, ["incident_type = ? AND machine_id = ?", @info_incident.incident_type, @info_incident.machine_id])
        if @incident
          message = "Mis à jour effectuée"
        else
          message = "problème lors de la mise à jour"
        end  
        flash[:notice] = message
        
        redirect_to :controller => "incidents", :action => "index"
    end
    
    def createCSV
      
        @bizarres = Incident.all(:conditions => ["machine_id = #{params[:machine]}"])
        date = Date.today.strftime('%m_%Y')
        heure = Time.now.strftime('%H-%M-%S')
        path = 'public/download'
        @nomFichierTxt = 'public/download/pc' + params[:machine].to_s + '-' + heure.to_s + '.txt'
        @nomFichierFinal = path + '/pc' + params[:machine].to_s + '-' + heure.to_s + '.csv'
        @fileName = 'pc' + params[:machine].to_s + '-' + heure.to_s + '.csv'
        
        fic = File.open(@nomFichierTxt.to_s, "w")
        fic.write "machine_id;nom_machine;type_incident;ram_use;ram_total;ram_percent;disk_use;disk_total;disk_percent;resolution_date;created_at;updated_at\n" 

      @bizarres.each do |bibi| 
         fic.write "#{bibi.machine_id.to_s};#{bibi.machine.machine_name.to_s};#{bibi.incident_type.to_s};#{bibi.ram_use.to_s};#{bibi.ram_total.to_s};#{bibi.ram_percent.to_s};#{bibi.disk_use.to_s};#{bibi.disk_total.to_s}; #{bibi.disk_percent.to_s};#{bibi.resolution_date.to_s};#{bibi.created_at.to_s};#{bibi.updated_at.to_s}\n"
       end 

       fic.close  
       File.rename(@nomFichierTxt, @nomFichierFinal)
       
      #send_file(@nomFichierFinal ,
          #  :filename      =>  'pc' + params[:machine].to_s + '-' + heure.to_s + '.csv',
          #  :type          =>  'application/csv',
          #  :disposition  =>  'attachment',
          #  :content-type => application/force-download
         #   :streaming    =>  'true',
        #:buffer_size  =>  '4096')
      send_file @nomFichierFinal, :type => 'application/csv', :disposition => 'attachment', :filename =>  'pc' + params[:machine].to_s + '-' + heure.to_s + '.csv'
    end
    
end
